﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.ZhiWu;
using Enterprise.Data.ZhiWu;
using Enterprise.Infrastructure;
using System.Data;
namespace Enterprise.Service.ZhiWu
{
    /// <summary>
    /// 用户职务对应关系
    /// </summary>
    public class UserZhiwuService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-UserZhiwu";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<UserZhiwuEntity> UserZhiWuList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<UserZhiwuEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                UserZhiwuData data = new UserZhiwuData();
                List<UserZhiwuEntity> list = data.List();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回用户职务对应关系
        /// </summary>
        /// <param name="zwId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool UserZhiWuCheck(int zwId, int userId)
        {
            bool rbool = false;
            var q= UserZhiWuList().Where(p => p.ZWID == zwId && p.USERID == userId).FirstOrDefault();
            if (q != null)
            {
                rbool = true;
            }
            return rbool;
        }

        /// <summary>
        /// 返回记录信息
        /// </summary>
        /// <param name="Id">编码</param>
        /// <returns></returns>
        public UserZhiwuEntity UserZhiWuDisp(int Id)
        {
            return UserZhiWuList().Where(p => p.UZID == Id).FirstOrDefault();
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void UserZhiWuExecute(UserZhiwuEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_USERZHIWU(UZID,ZWID,USERID) values(SEQ_SYS_USERZHIWU.nextval," + entity.ZWID + "," + entity.USERID + ")";
                    break;
                case "Update":
                    cmd = "Update SYS_USERZHIWU set ZWID=" + entity.ZWID + " , USERID=" + entity.USERID + " where UZID=" + entity.UZID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_USERZHIWU where UZID=" + entity.UZID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                UserZhiWuExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void UserZhiWuExecuteBySql(string cmd, int typeId)
        {
            UserZhiwuData data = new UserZhiwuData();
            data.Execute(cmd);
            if (typeId == 1)
            {
                CacheHelper.RemoveCache(cacheKey);
            }
        }

    }
}
